<template>
  <div id="search">
    <input
      type="text"
      v-model="value"
      @input="searchInputEve"
      @blur="isshow = false"
      @focus="value.length >= 1 ? (isshow = true) : ''"
    />
    <transition name="scale">
      <div v-show="isshow" class="searchEnd">
        <p>文章</p>
        <ul v-show="list.length != 0">
          <li v-for="(item, i) in list" :key="i">
            <a :style="{ color: red }" @click="goto(item)">{{ item.title }}</a>
          </li>
        </ul>
        <p v-show="list.length == 0">没有搜索到~</p>
        <!-- <transition name="slide"> -->
        <i v-show="isshowlott">
          <lottie-animation
            ref="anim"
            :animationData="require('@/assets/icon/loading.json')"
            :loop="true"
            :speed="1"
            style="width: 100%; height: 100%"
          />
        </i>
        <!-- </transition> -->
      </div>
    </transition>
    <!-- <div v-show="isshow" class="searchEnd"></div> -->
  </div>
</template>
<script>
import { SEARCH } from "@/api/Article";
export default {
  name: "",
  data() {
    return {
      value: "",
      list: {
        title: [{ title: "测试" }, { title: "测试1" }, { title: "测试2" }],
        tag: [
          { name: "html" },
          { name: "css" },
          { name: "前端" },
          { name: "后端" },
        ],
      },
      isshow: false,
      isblur: true,
      isshowlott: false,
    };
  },
  watch: {
    value(val) {
      console.log(val, this.isblur, val.length >= 1 && this.isblur);
      val.length >= 1 && this.isblur
        ? (this.isshow = true)
        : (this.isshow = false);
    },
  },
  methods: {
    searchInputEve() {
      this.isshowlott = true;
      let val = this.value;
      setTimeout(() => {
        if (val == this.value && this.value != "") {
          SEARCH({ title: this.value }).then((res) => {
            console.log("搜索结果", res);
            this.list = res.flag;
            this.isshowlott = false;
          });
        }
      }, 1000);
    },
    goto(item) {
      localStorage.setItem("item", JSON.stringify(item));
      localStorage.setItem("is", true);
      this.$router.push({ name: "ArticleDetailed" });
    },
  },
};
</script> <style lang='scss' scoped>
#search {
  margin-bottom: 30px;
  display: flex;
  align-items: flex-end;
  flex-direction: column;
  justify-content: center;
  position: relative;
  padding-right: 30px;
  input {
    width: 35px;
    height: 35px;
    border: none;
    outline: none;
    border-radius: 50%;
    transition: all 0.5s;
    padding-left: 10px;
    animation: searchKey 1s infinite alternate;
    background-color: #84d0ef;
    background-image: url("~@/assets/icon/search.png");
    background-size: 25px;
    background-repeat: no-repeat;
    background-position: center;
    box-shadow: 0 0 2px 10px #b4e2f5;
    box-sizing: border-box;
    margin-left: 40px;
    color: transparent;
    caret-color: #f89f85;
    font-size: 13px;
    letter-spacing: 1px;
    &:focus {
      // max-width: 400px;
      // min-width: 250px;
      width: 300px;
      border-radius: 4px;
      animation: none;
      box-shadow: none;
      background-color: #fff;
      color: #333;
    }
  }
  .searchEnd {
    width: 300px;
    min-height: 200px;
    background-color: #fff;
    margin-top: 10px;
    position: absolute;
    top: 40px;
    z-index: 999;
    box-shadow: 0px 0px 5px -1px #333;
    padding: 10px 0;
    padding: 10px;
    box-sizing: border-box;
    i {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      background-color: #fff;
    }
    ul {
      li {
        a {
          display: block;
          width: 280px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          margin: 5px 0;
          cursor: pointer;
          &:hover {
            color: #f89f85;
          }
        }
      }
    }

    p {
      font-size: 13px;
      color: #999;
    }
  }
}
@keyframes searchKey {
  0% {
    transform: scale(1);
  }
  100% {
    transform: scale(1.2);
  }
}
</style>